package com.wanhe.viewconf.views.base.video;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;

import java.io.IOException;

/**
 * File Name:VideoViewLayout.java<br/>
 * Package Name:com.wanhe.viewconf.views.base.video<br/>
 * Date:2017/7/12 16:06<br/>
 * author:chendeji<br/>
 * desc: 视频控件的内部实现<br/>
 * Copyright (c) 2017/7/12, 781571323@qq.com All Rights Reserved.
 *
 */
public class VideoViewLayout extends ViewGroup implements SurfaceHolder.Callback {

	private SurfaceView	mSurfaceView;
	private MediaPlayer	mMediaPlayer;
	private String		path;

	public VideoViewLayout(Context context) {
		this(context, null);
	}

	public VideoViewLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	private void initView(Context context) {
		// 需要重新的添加一个suface
		mSurfaceView = new SurfaceView(context);
		mSurfaceView.getHolder().addCallback(this);
		this.addView(mSurfaceView);
		mMediaPlayer = new MediaPlayer();
		mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
			@Override
			public void onPrepared(MediaPlayer mp) {
				mp.start();
				mp.setLooping(true);
			}
		});
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		mSurfaceView.layout(0, 0, r - l, b - t);
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		mMediaPlayer.setDisplay(holder);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
			try {
				holder.removeCallback(this);
				mMediaPlayer.pause();
				mMediaPlayer.stop();
				mMediaPlayer.release();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			}
		}
	}

	public void setPlayPath(String path) {
		if (TextUtils.isEmpty(path))
			return;
		this.path = path;
		try {
			mMediaPlayer.reset();
			mMediaPlayer.setDataSource(path);
			mMediaPlayer.prepareAsync();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void play() throws Exception {
		if (TextUtils.isEmpty(path))
			throw new Exception("must setUrlResource");
		try {
			mMediaPlayer.start();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		}
	}

	public void stop() {
		if (mMediaPlayer != null) {
			try {
				mMediaPlayer.stop();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			}
		}
	}

	public void pause() {
		if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
			mMediaPlayer.pause();
		}
	}

	public boolean isPlaying() {
		if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
			return true;
		} else {
			return false;
		}
	}

	MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
	public Bitmap getFrameAtFrist(String path) {
		mediaMetadataRetriever.setDataSource(path);
		return mediaMetadataRetriever.getFrameAtTime();
	}

}
